using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Xml;
using Model;
namespace MizService
{
	public class Service1 : System.ServiceProcess.ServiceBase
	{
		public static string Name = "MizaniyaService";
		private static EventLog evt = new EventLog("Application");

		public static string ConfigFile=AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Mizaniya.config";
		public static string EndPoint;
		public static string ServerPort;
		public static string baseDir = System.AppDomain.CurrentDomain.BaseDirectory;
		public static string DbSchemaFile = baseDir;
		public static string DbFile = baseDir;

		/// <summary>
		/// writes to the event log
		/// </summary>
		/// <param name="text"></param>
		public static void writeLog(string text)
		{
			try
			{
				evt.WriteEntry(Service1.Name+" "+text);
			} 
			catch (Exception e)
			{
				//do nothing: silently fail
			}


		}

		#region autogenerated
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Service1()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
		}

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
			this.ServiceName = Service1.Name; // Service name
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		// The main entry point for the process
		static void Main()
		{
			evt.Source = Service1.Name;

#if (DEBUG)
			
			
			Service1 myFakeService = new Service1();
			myFakeService.OnStart(null);
			while(true) {System.Threading.Thread.Sleep(100);}
#else
		
			
			#region autogenerated
			System.ServiceProcess.ServiceBase[] ServicesToRun;
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
			#endregion
			
#endif
			
			
			
			

			

		}



		/// <summary>
		/// For our purposes, this is the main function.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			// The config file must be present before starting the server
			XmlDocument doc = new XmlDocument();	// TODO: if the document is not available, exit
			doc.Load(ConfigFile);
			EndPoint = doc.SelectSingleNode("/myMizaniya/endPoint").InnerText;
			ServerPort = doc.SelectSingleNode("/myMizaniya/port").InnerText;
			DbFile += doc.SelectSingleNode("/myMizaniya/server/dbFile").InnerText;
			DbSchemaFile += doc.SelectSingleNode("/myMizaniya/server/dbSchemaFile").InnerText;

			// create the database manager
			DatabaseManagerADO myDB= new DatabaseManagerADO(DbFile,DbSchemaFile);

			// start the server
			HttpChannel chan = new HttpChannel( int.Parse(ServerPort) );
			ChannelServices.RegisterChannel( chan );
			RemotingServices.Marshal(myDB,EndPoint,typeof(DatabaseManagerADO));

#if (!DEBUG)
			writeLog("initialization complete");
#endif
		}


 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			// Bring down the server connection
			// ensure that the dataset is written to the datastorage

#if (!DEBUG)
			writeLog("stopping");
#endif
		}
	}
}
